// components/abc/abc.js\
const util = require('../../utils/util')
let timer
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    time: {
      type: String
    },
    exist:{
      type:Boolean
    }
  },
  //生命周期函数
  lifetimes: {
    ready() {
      this.getTimeFn()
      console.log(this.properties.exist)
    },
    detached(){
      clearInterval(timer)
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    txt: '标记为已读'
  },

  /**
   * 组件的方法列表
   */
  methods: {
    btnFn() {
      this.triggerEvent('num', 123)
    },
    getTimeFn() {
      let timeNum = this.properties.time;
      if(this.properties.exist){
        return
      }
      this.numFn(timeNum)
      timer = setInterval(() => {
        timeNum--
        this.numFn(timeNum)
        if (timeNum == 0) {
          clearInterval(timer)
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 2000
          })
          this.setData({
            txt:'标记为已读'
          })
          this.triggerEvent('finished')
        }
      }, 1000)
    },
    numFn(val) {
      let num = util.formatNumber(Math.floor(val / 60))
      let a = util.formatNumber(val % 60)
      this.setData({
        txt: num + ':' + a + '后标记为已读'
      })
    }
  }
})